#! /usr/bin/python
#coding:utf-8

import redis
import json
import sys

from tdm_config import *

reload(sys)
sys.setdefaultencoding('utf8')

def redis_set_to_json(r, rd_set):
    pass

def redis_hash_to_json(r, rd_hash):
    pass

def tdm_server_to_json():
    pass

def json_db_to_user(r, user_dict):
    for uname, uinfo in user_dict.items():
        if not uname.encode('utf-8') in r.hkeys(tdm_redis_key_user):
            #print 'add new user', uname
            r.hset(tdm_redis_key_user, uname.encode('utf-8'), uinfo)

def json_db_to_alert(r, alert_dict):
    for alt_name, alt_val in alert_dict.items():
        r.hset(tdm_redis_key_alert, alt_name.encode('utf-8'), alt_val.encode('utf-8'))

def json_db_to_jxd(r, jxd_dict):
    for city, jxd_list in jxd_dict.items():
        jxd_list_u8 = [jxd.encode('utf-8') for jxd in jxd_list]
        _jxd_list_old = r.hget(tdm_redis_key_jxd, city.encode('utf-8'))
        jxd_list_old = _jxd_list_old and _jxd_list_old.split(',') or []
        if not city.encode('utf-8') in r.hkeys(tdm_redis_key_jxd):
            #print 'add new city', city
            #print 'add new jxd', jxd_list_u8
            r.hset(tdm_redis_key_jxd, city.encode('utf-8'), ','.join(jxd_list_u8))
        else:
            for new_jxd in jxd_list_u8:
                if not new_jxd in jxd_list_old:
                    #print 'add new jxd', new_jxd
                    jxd_list_old.append(new_jxd)
            r.hset(tdm_redis_key_jxd, city.encode('utf-8'), ','.join(jxd_list_old))

def json_db_to_server(r, server_dict):
    for city_jxd_serv, server_attr in server_dict.items():
        city_jxd_serv = city_jxd_serv.encode('utf-8')
        server_attr = server_attr.encode('utf-8').replace("'", '"')
        if not city_jxd_serv in r.hkeys(tdm_redis_key_server):
            #print 'add new server deivce', city_jxd_serv, server_attr
            r.hset(tdm_redis_key_server, city_jxd_serv, server_attr)
        else:
            server_attr_old = r.hget(tdm_redis_key_server, city_jxd_serv)
            if not server_attr_old == server_attr:
                #print 'replace old server device', city_jxd_serv, server_attr
                r.hset(tdm_redis_key_server, city_jxd_serv, server_attr)

def json_db_to_network(r, network_dict):
    for city_jxd_dev, netdev_attr in network_dict.items():
        city_jxd_dev = city_jxd_dev.encode('utf-8')
        netdev_attr = netdev_attr.encode('utf-8').replace("'", '"')
        if not city_jxd_dev in r.hkeys(tdm_redis_key_network):
            #print 'add new network device', city_jxd_dev, netdev_attr
            r.hset(tdm_redis_key_network, city_jxd_dev, netdev_attr)
        else:
            netdev_attr_old = r.hget(tdm_redis_key_network, city_jxd_dev)
            if not netdev_attr_old == netdev_attr:
                #print 'replace old network device', city_jxd_dev, netdev_attr
                r.hset(tdm_redis_key_network, city_jxd_dev, netdev_attr)

def json_db_to_server_config(r, server_cfg_dict):
    for server_cfg, cfg_content in server_cfg_dict.items():
        server_cfg = server_cfg.encode('utf-8')
        cfg_content = cfg_content.encode('utf-8').replace("'", '"')
        if not server_cfg in r.hkeys(tdm_redis_key_server_item_cfg):
            #print 'add new config-of-server', server_cfg
            r.hset(tdm_redis_key_server_item_cfg, server_cfg, cfg_content)
        else:
            #print 'add new config-of-server', server_cfg
            r.hset(tdm_redis_key_server_item_cfg, server_cfg, cfg_content)

def json_db_to_network_config(r, network_cfg_dict):
    for network_cfg, cfg_content in network_cfg_dict.items():
        network_cfg = network_cfg.encode('utf-8')
        cfg_content = cfg_content.encode('utf-8').replace("'", '"')
        if not network_cfg in r.hkeys(tdm_redis_key_network_item_cfg):
            #print 'add new config-of-network', network_cfg
            r.hset(tdm_redis_key_network_item_cfg, network_cfg, cfg_content)
        else:
            #print 'update new config-of-network', network_cfg
            r.hset(tdm_redis_key_network_item_cfg, network_cfg, cfg_content)

def json_db_to_redis(r, file_name):
    json_str = open(file_name, 'r').read()
    json_data = json.loads(json_str)
    ##print json_data

    json_db_to_user(r, json_data.get(tdm_redis_key_user))
    json_db_to_alert(r, json_data.get(tdm_redis_key_alert))
    json_db_to_jxd(r, json_data.get(tdm_redis_key_jxd))
    json_db_to_server(r, json_data.get(tdm_redis_key_server))
    json_db_to_network(r, json_data.get(tdm_redis_key_network))
    json_db_to_server_config(r, json_data.get(tdm_redis_key_server_item_cfg))
    json_db_to_network_config(r, json_data.get(tdm_redis_key_network_item_cfg))

def json_db_clean(r):
    db_keys = r.keys()
    for key in db_keys:
        #print 'del redis key', key
        r.delete(key)

if __name__ == '__main__':
    r = redis.Redis(host=tdm_redis_host, db=tdm_redis_db)
    json_db_clean(r)
    json_db_to_redis(r, 'tdm_database.json')
    r.save()

